package org.example.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

//@Configuration
public class MybatisConfig {
    /*
    MyBatis的主配置文件
    都配置什么？
    配置数据源--已经在jdbcConfig配置完毕了

    我们主要做这两件事即可
    配置实体类别名
    配置mapper注册
     */

    //配置别名
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean ssfBean = new SqlSessionFactoryBean();
        ssfBean.setTypeAliasesPackage("org.example.entity");
        ssfBean.setDataSource(dataSource);
        //复杂的sql直接写在AccountMapper.xml映射文件里 需要有如下配置
        ssfBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return ssfBean;

    }


    //配置mapper注册
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc= new MapperScannerConfigurer();
        msc.setBasePackage("org.example.mapper");
        return msc;
    }
}
